<template>
  <div class="login-bg">
    <div class="login-container">
      <h2 class="login-title">应急任务待办系统</h2>
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm" class="login-form" label-width="0">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" placeholder="用户名" prefix-icon="el-icon-user" />
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="loginForm.password" type="password" placeholder="密码" prefix-icon="el-icon-lock" />
        </el-form-item>
        <el-form-item v-if="captchaEnabled" prop="code">
          <el-row :gutter="10">
            <el-col :span="16">
              <el-input v-model="loginForm.code" maxlength="4" placeholder="验证码" prefix-icon="el-icon-circle-check" />
            </el-col>
            <el-col :span="8">
              <img :src="codeUrl" @click="getCode" class="login-captcha" />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="login-btn" :loading="loading" @click.native.prevent="handleLogin">登 录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getCodeImg } from "@/api/login"
import Cookies from "js-cookie"
import { encrypt, decrypt } from '@/utils/jsencrypt'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: '',
        code: ''
      },
      loginRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        code: [{ required: true, message: '请输入验证码', trigger: 'blur' }]
      },
      loading: false,
      captchaEnabled: false,
      codeUrl: '',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
    this.getCode()
    this.getCookie()
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.captchaEnabled === undefined ? true : res.captchaEnabled
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.img
          this.loginForm.uuid = res.uuid
        }
      })
    },
    getCookie() {
      const username = Cookies.get("username")
      const password = Cookies.get("password")
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      }
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          if (this.loginForm.rememberMe) {
            Cookies.set("username", this.loginForm.username, { expires: 30 })
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 })
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 })
          } else {
            Cookies.remove("username")
            Cookies.remove("password")
            Cookies.remove('rememberMe')
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || "/" }).catch(()=>{})
          }).catch(() => {
            this.loading = false
            if (this.captchaEnabled) {
              this.getCode()
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.login-bg {
  min-height: 100vh;
  width: 100vw;
  background: linear-gradient(135deg, #2193b0 0%, #6dd5ed 100%);
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-container {
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.2);
  padding: 48px 36px 36px 36px;
  min-width: 340px;
  max-width: 90vw;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login-title {
  font-size: 2rem;
  font-weight: bold;
  color: #2193b0;
  margin-bottom: 32px;
  letter-spacing: 2px;
}
.login-form {
  width: 100%;
}
.el-input__inner {
  border-radius: 8px;
  height: 40px;
  font-size: 16px;
}
.el-form-item {
  margin-bottom: 24px;
}
.login-btn {
  width: 100%;
  border-radius: 8px;
  height: 42px;
  font-size: 18px;
  background: linear-gradient(90deg, #2193b0 0%, #6dd5ed 100%);
  border: none;
  color: #fff;
  font-weight: bold;
  transition: box-shadow 0.2s;
}
.login-btn:hover {
  box-shadow: 0 4px 16px 0 rgba(33,147,176,0.15);
}
.login-captcha {
  width: 100%;
  height: 40px;
  border-radius: 8px;
  cursor: pointer;
  border: 1px solid #eee;
  object-fit: cover;
}
</style>
